Ext.onReady(function(){ 

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var sm = new Ext.grid.CheckboxSelectionModel();

var cm1=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'客户号',dataIndex:'custNo'},
{header:'客户名称',dataIndex:'custName',width:100},
{header:'所属行业',dataIndex:'industryType',width:100},
{header:'客户类型',dataIndex:'custType',width:100,sortable:true},
{header:'信用等级',dataIndex:'creditLevel',width:80,sortable:true},
{header:'风险暴露分类',dataIndex:'exposureType',width:100,sortable:true},
{header:'评级认定日期',dataIndex:'confirmDate',width:120,sortable:true},
{header:'评级状态',dataIndex:'phaseStat',width:120,sortable:true},
{header:'违约状态',dataIndex:'defaultStat',width:100,sortable:true},
{header:'信贷余额(元)',dataIndex:'balance',width:100,sortable:true},
{header:'所属机构',dataIndex:'inputOrgId',width:120},
{header:'经办人',dataIndex:'inputUserName',width:100},
{header:'客户规模',dataIndex:'custScope',width:100,hidden:true},
]); 
var ds=new Ext.data.Store({ 
				reader:new Ext.data.JsonReader({ },[
				{name:'custNo'},
				{name:'custName'}, 
				{name:'custType'},
				{name:'organizationId'},
				{name:'busiLicenseId'},
				{name:'industryType'},
				{name:'custScope'},
				{name:'creditLevel'},
				{name:'confirmDate'},
				{name:'inputOrgId'},
				{name:'balance'},
				{name:'exposureType'},
				{name:'phaseStat'},
				{name:'defaultStat'},
				{name:'inputUserName'},
				
				]) 
});

var modelStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showPDModelList.do',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
});
modelStore.load();

var custTypeStore = new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=CustType',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
});

var phaseStatStore = new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=PhaseStat',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
});

var defaultStatStore = new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=DefaultStat',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
	});

var custScopeStore = new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=CustScope',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
});
//
//var industryTypeStore = new Ext.data.Store({ 
//	proxy:new Ext.data.HttpProxy({
//	url:'showType.do?type=IndustryType',
//	method:'GET'
//	}), 
//	reader:new Ext.data.JsonReader({ },[
//	{name:'name'}, 
//	{name:'code'}
//	]) 
//});
var addIndustryTypeLevelOneStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showIndustryTypeLevelOne.do',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) ,
	autoLoad:true
	}); 

	var addIndustryTypeLevelTwoStore=new Ext.data.Store({  
		reader:new Ext.data.JsonReader({ },[
		{name:'name'}, 
		{name:'code'}
		])
		}); 

var exposureTypeStore = new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=ExposureType',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
});
var pdValueStore = new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=PDValue',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
});

var orgTypeStore = new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=OrgType',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
});


var grid=new Ext.grid.GridPanel({ 
bodyStyle: 'padding:5px',
id:'myCustomers',
renderTo:'result_panel',
stripeRows:true,//斑马线效果
title:"查询结果列表", 
height:460, 
cm:cm1,
store: ds, 
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 20,   
                store: ds,   
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            })  ,
            tbar:[
          		{text:'导出excel',handler:exportExcel},
          	]           
});

//查找客户表单
var form = new Ext.form.FormPanel({ 
renderTo:'search_panel',     
height : 270,
width:900,
id: 'findForm', 
title: '查询客户',
anchor:'80%', 
bodyStyle: 'margin:3px;text-align:left;', 
frame: true, 
labelWidth: 100, 
buttonAlign:'right',
items: 
[ {
	layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
        	   xtype: 'textfield', 
        	   id:'custName',
        	   name: 'custName', 
        	   fieldLabel: '客户名称',  
        	   allowBlank: true, 
        	   width:250
}]},{ columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
        	   xtype: 'textfield', 
        	   id:'custNo',
        	   name: 'custNo', 
        	   fieldLabel: '客户号',  
        	   allowBlank: true, 
        	   width:250
         
}]} 
]},{
	layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
	xtype: 'combo', 
	 id : "custType",
	 store :custTypeStore,
	fieldLabel: '客户类型',
	valueField : 'code',
	displayField : 'name',
	emptyText : '请选择客户类型...',
	allowBlank : true,
	typeAhead : true,
	width: 250,
	mode:'remote',  
	hiddenName:'custType',
	editable: false,
	triggerAction : 'all',
	selectOnFocus : true   
	}]},{ columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
	        	   xtype: 'combo', 
	      		 id : "custScope",
	      		 store :custScopeStore,
	      		fieldLabel: '客户规模',
	      		valueField : 'code',
	      		displayField : 'name',
	      		emptyText : '请选择客户规模...',
	      		allowBlank : true,
	      		typeAhead : true,
	      		width: 250,
	      		mode:'remote',  
	      		hiddenName:'custScope',
	      		editable: false,
	      		triggerAction : 'all',
	      		selectOnFocus : true  
	         
	}]} 
	]},
	{layout:"column",
		items:[{
			columnWidth : .5, // 该列有整行中所占百分比
			layout : "form",
			items : [{
				xtype: 'combo', 
		 		id : "IndustryTypeLevelOne",
		       store :addIndustryTypeLevelOneStore,
		       fieldLabel: '行业大类',
		       valueField : 'code',
		       displayField : 'name',
		       typeAhead : true,
		       width: 250,
		       mode:'remote', 
		       emptyText : '请选择行业大类型...',
		       editable: false,
		       triggerAction : 'all',
		       selectOnFocus : true,
		       hiddenName:'industryTypeLevelOne' ,
		       listeners:{
		           select : function(combo, record,index)   
		           {
					var industryTypeLevelTwoCombo = Ext.getCmp('IndustryTypeLevelTwo');
					industryTypeLevelTwoCombo.clearValue(); //可以实现当下拉值变更时，清空之前下拉选项中的值   
					addIndustryTypeLevelTwoStore.proxy =new Ext.data.HttpProxy({
					url:'showIndustryTypeLevelTwo.do?LevelOneId='+combo.value,
					method:'GET'
					}); 
					addIndustryTypeLevelTwoStore.load(); //加载下拉框的store      
		           }      
		       }
	}]},{
			columnWidth : .5, // 该列有整行中所占百分比
			layout : "form",
		    items : [{
		    	xtype: 'combo', 
		 		id : "IndustryTypeLevelTwo",
		       store :addIndustryTypeLevelTwoStore,
		       fieldLabel: '行业子类',
		       valueField : 'code',
		       displayField : 'name',
		       typeAhead : true,
		       width: 250,
		       mode:'remote', 
		       emptyText : '请选择行业子类型...',
		       editable: false,
		       triggerAction : 'all',
		       selectOnFocus : true,
		       hiddenName:'industryTypeLevelTwo'        
		           
		}
		]} 
		]},
		
{
	layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
        	   xtype: 'combo', 
      		 id : "phaseStat",
      		 store :phaseStatStore,
      		fieldLabel: '评级状态',
      		valueField : 'code',
      		displayField : 'name',
      		emptyText : '请选择评级状态...',
      		allowBlank : true,
      		typeAhead : true,
      		width: 250,
      		mode:'remote',  
      		hiddenName:'phaseStat',
      		editable: false,
      		triggerAction : 'all',
      		selectOnFocus : true  
		 
}]},{ columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
        	   xtype: 'combo', 
        	   id : "exposureType",
        	   store :exposureTypeStore,
        	  fieldLabel: '风险暴露分类',
        	  valueField : 'code',
        	  displayField : 'name',
        	  emptyText : '请选择风险暴露分类...',
        	  allowBlank : true,
        	  typeAhead : true,
        	  width: 250,
        	  mode:'remote',  
        	  hiddenName:'exposureType',
        	  editable: false,
        	  triggerAction : 'all',
        	  selectOnFocus : true    
}]} 
]},
{
	layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
		xtype: 'combo', 
		 id : "pdValue",
		 store :pdValueStore,
		fieldLabel: '信用等级',
		valueField : 'code',
		displayField : 'code',
		emptyText : '请选择信用等级...',
		allowBlank : true,
		typeAhead : true,
		width: 250,
		mode:'remote',  
		hiddenName:'pdValue',
		editable: false,
		triggerAction : 'all',
		selectOnFocus : true   
}]},{ columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
        	   xtype: 'combo',
				id: 'orgCode',
				//name: 'exposureType', 
				fieldLabel: '所属机构', 
				store:new Ext.data.SimpleStore({fields:['id', 'text'],data:[[]]}), 
				displayField: 'text',
				valueField: 'id',
				hiddenName: 'orgCode',
	            editable:false, 
	            mode: 'local', 
	            triggerAction:'all', 
	            frame:true,
	            border:true,
	            width:250,
	            tpl: '<div id="treePanel" style="height:180px;width:240px;"></div>', //html代码      
	            emptyText:'请选择客户所属机构...',
	            resizable:true
}]} 
]},{
	layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{  
	        	   xtype: 'combo', 
	        	   id : "modelId",
	              store :modelStore,
	              fieldLabel: '打分卡模板',
	              valueField : 'code',
	              displayField : 'name',
	              editable: false,
	              allowBlank: true,
	              emptyText: '请选择打分卡模板',
	              typeAhead : true,
	              width: 250,
	              mode:'remote',  
	              triggerAction : 'all',
	              selectOnFocus : true,
	              hiddenName:'modelId',
	}]},{ columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
	        	   xtype: 'combo',
	   			id: 'defaultStat',
	   			//name: 'exposureType', 
	   			fieldLabel: '违约状态',
	        	   store :defaultStatStore, 
	         	  valueField : 'code',
	       	  displayField : 'name',
	       	  emptyText : '请选择违约状态...',
	       	  allowBlank : true,
	       	  typeAhead : true,
	       	  width: 250,
	       	  mode:'remote',  
	       	  hiddenName:'defaultStat',
	       	  editable: false,
	       	  triggerAction : 'all',
	       	  selectOnFocus : true
	}]} 
	]},
	{
		layout:"column",
		items:[{
			columnWidth : .5, // 该列有整行中所占百分比
		           layout : "form",
		           items : [{
		        	   xtype: 'datefield',
						id: 'classifyDateStart', 
						name: 'classifyDateStart', 
						fieldLabel: '认定日期区间（起始）',
						allowBlank: true,
						editable:false,  
						format : 'Y-m' ,
						width: 250 
		}]},{ columnWidth : .5, // 该列有整行中所占百分比
		           layout : "form",
		           items : [{
		        	   xtype: 'datefield',
		        	   id: 'classifyDateEnd', 
						name: 'classifyDateEnd', 
						fieldLabel: '认定日期区间（截止）',
						allowBlank: true,
						editable:false,  
						format : 'Y-m' ,
						width: 250
		}]} 
		]},
],
buttons:[{text:"查询", handler: findCustomer},
{text:"重置", handler: function(){ Ext.getCmp('findForm').form.reset();orgCode = "";}}]
});

var tree = new Ext.tree.TreePanel
({ 
    animate:true,
    titleCollapse:true,
    root:root,
    singleExpand:false,
    iconCls:"icon-tree",
    autoLoad:false,
    allowDomMove:true,
    frame:true,
    collapsible:false,
    collapsed:false,
    collapseFirst:true,
    border:false,
    listeners:{ 
    	render:function(tree){ 
    		tree.el.on('click',function(e){ 
			e.stopPropagation(); }) ;
		} 
	} 
});

var loader=new Ext.tree.TreeLoader({dataUrl:'system/showOrgTree.do'});

var root=new Ext.tree.AsyncTreeNode
({
 id:"0", 
 leaf:false,
 loader: loader,
 text:"清远农商行总行",
 expandable:true,
 expanded:true 
});         

tree.setRootNode(root);

var comboTree =  Ext.getCmp('orgCode');      
var orgCode = "";
comboTree.on('expand',function(){tree.render('treePanel');});

var tree_click = function(node,e)
{
	 e.stopEvent();   //非叶子节点则不触发
     comboTree.setValue(node.id);  //设置option值
     comboTree.setRawValue(node.text);
     //comboTree.hiddenField.value = node.id;  
 	comboTree.collapse();   //隐藏option列表
 	orgCode = comboTree.value;
};
tree.on("click",tree_click);  

//表单验证-都为空
Ext.apply(Ext.form.VTypes,   
{
 allNull: function(val, field){
 if(field.allNull){
 	var custTypeId = field.allNull.custType;
 	this.custTypeField = Ext.getCmp(custTypeId);
 	var custNameId = field.allNull.custName;
 	this.custNameField = Ext.getCmp(custNameId);
 	var orgId = field.allNull.organizationID;
 	this.orgIdField = Ext.getCmp(orgId);
 	var blId = field.allNull.busiLicenseID;
 	this.blIdField = Ext.getCmp(blId);
 	
 	var custTypeValue = this.custTypeField.getValue();
 	var custNameValue = this.custNameField.getValue();
 	var orgIdValue = this.orgIdField.getValue();
 	var blIdValue = this.blIdField.getValue();
 } 
 if(custTypeValue==null&&custNameValue==null&&orgIdValue==null&&blIdValue==null){
 	Ext.MessageBox.show({
                     title: '提示',
                     msg: '请输入至少一项查询条件！',
                     buttons: Ext.Msg.OK
                });
 	return false;
 }
 else{
 	return true;
 }
 }
});
//查询客户 
function findCustomer() { 
	var findForm = Ext.getCmp("findForm");
	//alert("查询");
	
	if(findForm.form.isValid()){
	 findForm.form.doAction('submit', { 
                    url : 'RiskReport/findCustomer.do?orgCode='+orgCode,
                    
                    method : 'post', 
                    success : function(form,action) { 
                    var resultCust = Ext.util.JSON.decode(action.response.responseText).result;
                    
                    if(JSON.stringify(resultCust)!='[]'){
                    ds.proxy = new Ext.data.MemoryProxy(resultCust);
					ds.load({params:{start:0,limit:20}});
					}
					else{
					ds.proxy = new Ext.data.MemoryProxy(resultCust);
					ds.load();
					Ext.Msg.alert('提示', '当前客户不存在！');}
                    }, 
                        failure : function() { 
						Ext.Msg.alert('提示', '请求异常，请重新查询！'); 
					} 
                    }); 
}
} ;
function exportExcel(){
	window.location.href='RiskReport/exportExcel.do?id=01';
};

});